package com.javabasic.algorithm.leetcode;

/**
 * @Author xiongmin
 * @Description //TODO
 * @Date 2020/1/13 12:55
 * @Version 1.0
 **/
public class CountAndSay {

    /**
     * @解法： 递归解法
     * @param n
     * @return
     */
    public String countAndSay(int n) {
        return getResultStr(1,n,"1");
    }

    private String getResultStr(int cur,int n, String s) {
        if (cur == n) return s;
        s = getStr(s);
        return getResultStr(cur+1,n,s);
    }

    private String getStr(String s) {
        if (s == null || s.isEmpty()) return s;
        int len = s.length();
        StringBuilder result = new StringBuilder();
        char pre = s.charAt(0);
        int temp = 1;
        for (int i = 1; i < len; i++) {
            if (s.charAt(i) == pre) {
                temp ++;
            } else {
                result.append((char)('0'+temp) + "" + pre);
                temp = 1;
                pre = s.charAt(i);
            }
        }
        // 要处理最后的字符
        result.append((char)('0'+temp) + "" + pre);
        return result.toString();

    }
}
